package com.kly.user.serviceImpl.chatter;

import com.alibaba.fastjson.JSON;
import com.fqgj.common.api.Page;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.DateUtil;
import com.fqgj.common.utils.StringUtils;
import com.jsoniter.JsonIterator;
import com.jsoniter.any.Any;
import com.jsoniter.output.JsonStream;
import com.kly.user.db.dao.*;
import com.kly.user.db.entity.ChatterWorkSettingEntity;
import com.kly.user.db.entity.UserAccountsEntity;
import com.kly.user.db.entity.UserChatterMappingEntity;
import com.kly.user.db.entity.UserEntity;
import com.kly.user.dto.chatter.Chatter;
import com.kly.user.dto.chatter.ChatterModel;
import com.kly.user.service.ChatterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;

@Service("chatterServiceV2")
public class ChatterServiceImpl implements ChatterService {

    @Autowired
    private UserDao userDao;

    @Autowired
    UserInterestsDao userInterestsDao;

    @Autowired
    UserProfileDao userProfileDao;

    @Autowired
    private UserChatterMappingDao userChatterMappingDao;


    @Autowired
    UserAlbumsDao userAlbumsDao;

    @Autowired
    private UserAccountsDao userAccountsDao;

    @Autowired
    private ChatterWorkSettingDao chatterWorkSettingDao;

    @Override
    public List<Chatter> getChatters(String params, Page page) {
        List<Chatter> res = new ArrayList<>();

        Map<String, Object> map = new HashMap<>();
        map.put("userType", 1);
        if (StringUtils.isNotEmpty(params)) {
            Any deserialize = JsonIterator.deserialize(params);
            if (deserialize != null) {
                Set<String> keys = deserialize.keys();
                if (keys.contains("nickName")) {
                    String nickName = deserialize.get("nickName").toString();
                    if (StringUtils.isNotEmpty(nickName)) map.put("nickName", nickName);
                }
                if (keys.contains("onlineStatus")) {
                    Integer onlineStatus = deserialize.get("onlineStatus").toInt();
                    map.put("onlineStatus", onlineStatus);
                }

                if (keys.contains("account")) {
                    String account = deserialize.get("account").toString();
                    if (StringUtils.isNotEmpty(account)) map.put("account", account);
                }
            }
        }

        List<UserEntity> userEntities = userDao.queryUsers(map, page);
        if (CollectionUtils.isNotEmpty(userEntities)) {
            List<String> userCodes = userEntities.parallelStream().map(UserEntity::getUserCode).collect(Collectors.toList());
            List<UserAccountsEntity> byUserCodes = userAccountsDao.getByUserCodes(userCodes);
            Map<String, UserAccountsEntity> accountsEntityMap = new HashMap<>();
            byUserCodes.forEach(userAccountsEntity -> {
                accountsEntityMap.put(userAccountsEntity.getUserCode(), userAccountsEntity);
            });
            Map<String, List<String>> chatterMappingEntityHashMap = new HashMap<>();
            List<UserChatterMappingEntity> userChatterMappingEntities = userChatterMappingDao.queryByChatterUserCodes(userCodes);
            userChatterMappingEntities.forEach(userChatterMappingEntity -> {
                List<String> chatterList = new ArrayList<>();
                if (chatterMappingEntityHashMap.containsKey(userChatterMappingEntity.getChatterUserCode())) {
                    chatterList = chatterMappingEntityHashMap.get(userChatterMappingEntity.getChatterUserCode());
                }

                chatterList.add(userChatterMappingEntity.getModelUserCode());
                chatterMappingEntityHashMap.put(userChatterMappingEntity.getChatterUserCode(), chatterList);

            });

            List<ChatterWorkSettingEntity> settingsByChatterCods = chatterWorkSettingDao.getSettingsByChatterCods(userCodes);

            userEntities.forEach(user -> {
                Chatter chatter = new Chatter();
                chatter.setChatterCode(user.getUserCode());
                chatter.setAccount(accountsEntityMap.get(user.getUserCode()).getAccount());
                chatter.setCreateDt(user.getCreateAt());
                chatter.setJobType(user.getWorkType() == null ? -1 : user.getWorkType());
                chatter.setNickName(user.getNickName());
                chatter.setOnlineStatus(user.getOnline());
                chatter.setStatus(user.getStatus() == null ? -1 : user.getStatus());
                chatter.setLastActiveDate(user.getOnlineTimestamp() == null ? null : new Date(Long.parseLong(user.getOnlineTimestamp())));
                chatter.setBindModelsCount(chatterMappingEntityHashMap.containsKey(user.getUserCode()) ? chatterMappingEntityHashMap.get(user.getUserCode()).size() : 0);
                chatter.setGender(user.getGender());
                res.add(chatter);
            });
        }

        return res;
    }

    public static void main(String[] args) {
        Date d = new Date(Long.parseLong("1692673093108"));
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sf.format(d);
        Date date = DateUtil.getDate(format, "yyyy-MM-dd HH:mm:ss");


        System.err.println(11);
    }

    @Override
    public List<ChatterModel> getChatterModels(String chatterCode) {
        return null;
    }

    @Override
    public void updateChatter(Chatter chatter, String extraInfo) {

    }

    @Override
    public void updateChatterModel(ChatterModel chatterModel, String extraInfo) {

    }

    @Override
    public void bindRelationUpdate(String chatterCode, List<String> modelCode, Integer action) {

    }
}
